# ------------------------------------- #
# Replication code for:
#
# Rathbun, Brian, Christopher Sebastian Parker, and Caleb Pomeroy "Separate but Unequal: Ethnocentrism and Racialization 
# Explain the 'Democratic' Peace in Public Opinion," American Political Science Review.
#
# This script reproduces our reanalyses of Allan Dafoe, Baobao Zhang, and Devin Caughey (2018) "Information Equivalence 
# in Survey Experiments," Political Analysis.
# ------------------------------------- #

# --- libraries --- #
library(psych)
library(DirectEffects)
library(texreg)
library(plyr)
library(dplyr)
set.seed(1912)

# --- set working directory
setwd("~/Downloads/replication_files/")

# --- load data
dafoe_etal <- readRDS("data/dafoe_etal.rds")
nrow(dafoe_etal)

# subjects assigned to democracy condition more likely to believe the hypothetical country 
# has a majority white population, as footnoted in the dataverse appendix
t.test(subset(dafoe_etal, regime_type == "nondemoc")$white_pop,
       subset(dafoe_etal, regime_type == "democ")$white_pop)

# --- ethnocultural dimension
dafoe_etal <- dafoe_etal[!is.na(dafoe_etal$regime_type_basic),]

ethnocultural_items <- cbind(dafoe_etal$white,
                    dafoe_etal$religion)
ethnocultural_fit <- fa(ethnocultural_items,nfactors=1, rotate="varimax",scores=TRUE, fm="pa")
ethnocultural_fit$loadings 
dafoe_etal$cultural_factor <- ethnocultural_fit$scores[,1]

# --- strategic dimension
strategic_items <- cbind(dafoe_etal$alliance,
                    dafoe_etal$gdp,
                    dafoe_etal$oil,
                    dafoe_etal$force,
                    dafoe_etal$trade,
                    dafoe_etal$exercise,
                    dafoe_etal$invest)
strategic_fit <- fa(strategic_items,nfactors=1, rotate="varimax",scores=TRUE, fm="pa")
dafoe_etal$strategic_factor <- strategic_fit$scores[,1]

# --- estimate effect of democracy fixing ethnocultural and strategic effects
summary(ate_mod <- lm(strike ~ regime_type_basic + age + sex + educ + partyid, dafoe_etal))

set.seed(123)
form_ethnocultural <- strike ~ regime_type_basic + age + sex + educ + partyid | strategic_factor | cultural_factor
ethnocultural_acde <- sequential_g(form_ethnocultural, dafoe_etal)
ethnocultural_boots <- boots_g(ethnocultural_acde, boots = 5000)
summary(ethnocultural_boots)

set.seed(123)
form_strategic <- strike ~ regime_type_basic + age + sex + educ + partyid | cultural_factor | strategic_factor
strategic_acde <- sequential_g(form_strategic, dafoe_etal)
strategic_boots <- boots_g(strategic_acde, boots = 5000)
summary(strategic_boots)

# --- format table for dataverse appendix
ate_table <- createTexreg(coef.names = names(coef(ate_mod)),
                            coef = coef(ate_mod),
                            ci.low = confint(ate_mod, level = .95)[,1],
                            ci.up = confint(ate_mod, level = .95)[,2])

ethnocultural_table <- createTexreg(coef.names = rownames(summary(ethnocultural_boots)),
                          coef = summary(ethnocultural_boots)[,1],
                          ci.low = summary(ethnocultural_boots)[,4],
                          ci.up = summary(ethnocultural_boots)[,5])

strategic_table <- createTexreg(coef.names = rownames(summary(strategic_boots)),
                                    coef = summary(strategic_boots)[,1],
                                    ci.low = summary(strategic_boots)[,4],
                                    ci.up = summary(strategic_boots)[,5])

# --- table B13
screenreg(list(ate_table, ethnocultural_table, strategic_table))

# --- store in DF to plot
plot_df <- data.frame(mean_est = c(coef(ate_mod)["regime_type_basicdemoc"], summary(ethnocultural_boots)[2,1], summary(strategic_boots)[2,1]),
                      ci_95_low = c(confint(ate_mod, "regime_type_basicdemoc", .95)[1], summary(ethnocultural_boots)[2,4], summary(strategic_boots)[2,4]),
                      ci_95_high = c(confint(ate_mod, "regime_type_basicdemoc", .95)[2], summary(ethnocultural_boots)[2,5], summary(strategic_boots)[2,5]),
                      variable = c("Effect of Democracy\n(ATE)", "Effect of Democracy,\nFixing Ethnocultural Concerns", "Effect of Democracy,\nFixing Strategic Concerns"))

percent_ethnocultural_decrease <- (plot_df$mean_est[1] - plot_df$mean_est[2])/plot_df$mean_est[1]
percent_strategic_decrease <- (plot_df$mean_est[1] - plot_df$mean_est[3])/plot_df$mean_est[1]
plot_df$variable <- factor(plot_df$variable, levels = c("Effect of Democracy,\nFixing Ethnocultural Concerns", "Effect of Democracy,\nFixing Strategic Concerns" , "Effect of Democracy\n(ATE)"))
# --- figure B3
ggplot(plot_df, aes(x = mean_est, y = variable, color = variable)) +
  geom_vline(xintercept = 0, size = .8, color = "black") +
  geom_linerange(aes(xmin = ci_95_low, xmax = ci_95_high), size = 1.8, color = "gray10") +
  geom_point(size = 5.5, color = "black") +
  geom_point(size = 4.5) +  
  coord_cartesian(xlim = c(-.8,.2)) +
  theme_minimal() +
  annotate("text", x=-.3, y=2.2, label= paste("-", round(percent_strategic_decrease*100, digits = 1), "%", sep = ""), size = 4.2, color = "black") +
  annotate("text", x=-.2, y=1.2, label= paste("-", round(percent_ethnocultural_decrease*100, digits = 1), "%", sep = ""), size = 4.2, color = "black") +
  scale_color_manual(values=c("#92978a", "#92978a", "gray30")) +
  #scale_color_manual(values = c("#8fbc8f", "gray70", "gray98")) +
  labs(x = "Effect on Support\nfor Preventive Strike", y = NULL) +
  theme(legend.title = element_blank(),
        axis.text.y = element_text(size = 15),
        axis.text.x = element_text(size = 14),
        axis.line.y.right = element_blank(),
        axis.title.x = element_text(size = 16, margin = margin(t=15)),
        plot.margin = margin(r=25, l=15, t=15, b=15),
        panel.grid.minor.x = element_blank(),
        legend.position = "none")


